package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Orders;
import com.example.entity.OrdersList;
import com.example.entity.ShopCar;
import com.example.entity.TimeData;
import com.example.service.OrdersService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("orders")
public class OrdersController {


    @Autowired
    private OrdersService ordersService;
    

    /**
     * 分页查询所有订单信息（管理员）
     * @return
     */

    @GetMapping("/getPageOrders/{currentPage}/{pageSize}")

    public R getPageOrders(@PathVariable int currentPage,@PathVariable int pageSize,Orders orders){

        IPage page=new Page(currentPage,pageSize);

        LambdaQueryWrapper<Orders> lqw =new LambdaQueryWrapper<>();
        lqw.like(orders.getUserId()!=null,Orders::getUserId,orders.getUserId());

        IPage page1 = ordersService.page(page,lqw);

        return R.success("分页查询所有orders成功！",page1);
    }

    /**
     * 分页查询当前用户的订单信息（用户）
     * @return
     */

    @GetMapping("/getPageOrderById/{currentPage}/{pageSize}")

    public R getPageOrderById(@PathVariable int currentPage,@PathVariable int pageSize,Orders orders){

        IPage page=new Page(currentPage,pageSize);

        LambdaQueryWrapper<Orders> lqw =new LambdaQueryWrapper<>();


        lqw.eq(orders.getUserId()==null,Orders::getUserId,orders.getUserId());

        IPage page1 = ordersService.page(page,lqw);

        return R.success("分页查询当前orders成功！",page1);
    }

    /**
     * 年营业额
     * @return
     */
    @GetMapping("/getTotalByYear")
    public R getTotalByYear(){
        List<TimeData> list=ordersService.getTotalByYear();

        return R.success("年营业额查询成功！",list);
    }

    /**
     * 月营业额
     * @return
     */
    @GetMapping("/getTotalByMonth")
    public R getTotalByMonth(){

        List<TimeData> list=ordersService.getTotalByMonth();
//        System.out.println("长度"+list.size());
//
//        for (int i = 0; i < list.size(); i++) {
//
//
//            //数据库获取的字段时间
//            String year = list.get(i).getYear();
//            System.out.println("+++===+++");
//            System.out.println(year);
//            System.out.println("+++===+++");
//            //判断是否是今年
//            if (year.equals("2023")){
//                SimpleDateFormat formatter2 = new SimpleDateFormat("MM");
//                Date time = list.get(i).getTime();
//                String time2 = formatter2.format(time);
//                //如果是今年的就存入月份
//                list.get(i).setMonth(time2);
//            }else {
//                //如果不是今年的就存入月份(删除这个数据)
//                list.remove(i);
//            }
//        }



        return R.success("月营业额查询成功！",list);
    }

    /**
     * 日营业额
     * @return
     */
    @GetMapping("/getTotalByDay")
    public R getTotalByDay(){
        List<TimeData> list=ordersService.getTotalByDay();

        return R.success("日营业额查询成功！",list);
    }

    /**
     * 添加
     * @param orders
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Orders orders){

        //获取当前 下单时间
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        orders.setTime(timeStamp);

        //随机生成8位数当作 订单号
        Integer num = (int) ((Math.random() * 9 + 1) * 100000000);
        String orderNumber = String.valueOf(num);
        orders.setOrderNumber(orderNumber);



        Boolean adds = ordersService.save(orders);

        if (adds){
            return R.success("添加orders成功!",orders);
        }
        return R.error("添加orders失败!");
    }
////
////    /**
////     * 通过id删除
////     * @param id
////     * @return
////     */
////    @DeleteMapping("/deleteOrder/{id}")
////    public R deleteById(@PathVariable int id){
////
////        Boolean byId = orderService.deleteById(id);
////        if (byId){
////            return R.success("删除order成功!",null);
////        }
////        return R.error("删除order失败!");
////    }
////
//    /**
//     * 修改
//     * @param order
//     * @return
//     */
//    @PutMapping("/updateOrder")
//    public R deleteById(@RequestBody Orders order){
//
//        Boolean update = ordersService.updateById(order);
//        if (update){
//            return R.success("修改order成功!",null);
//        }
//        return R.error("修改order失败!");
//    }
//
//
    public static void main(String[] args) {
//        Date date = new Date();
////        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        SimpleDateFormat formatter = new SimpleDateFormat("MM");
//
//        String format = formatter.format(date);
//
//        System.out.println(format);


        String str="2023-01-03";
        String substring = str.substring(0, 4);
        System.out.println(substring);
    }
}

